package com.opensky.qatest.ui.page;

import junit.framework.Assert;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.LoadableComponent;
import org.openqa.selenium.support.ui.Select;

import com.opensky.qatest.ui.component.OfferPageJoinModal;
import com.opensky.qatest.ui.component.OfferPageJoinNag;
import com.opensky.qatest.util.common.AssertReport;
import com.opensky.qatest.util.common.LogicUtils;
import com.opensky.qatest.util.selenium.WebDriverWrapper;

/*
 * OpenSky - "Offer page" model
 *
 * @author: Aliakse_Khursevich (hursevich@gmail.com)
 * @version: 1.0
 * 
 */
public class OfferPage extends LoadableComponent<OfferPage> implements IPage {

    // ************************ Locators ************************

    private static final String JOIN_MODAL = "//div[@id='ui-tooltip-modal' and contains(@style, 'display: block')]//div[@id='join-modal']";
    
    private static final String JOIN_NAG = "//form[contains(@action, '/join?osky_origin')]";	
    
    private static final String URL_PATTERN = "https://opensky.com/%s/product/%s";

    private static final String HELP_LINK = "//a[@href='/content/faq' and text()='Help']";

    private static final String LOGIN_LINK = "//div[@class='login']/a[text()='Log in']";

    // private static final String CART_LINK = "//div[@id='cart-nav']//span[@class='cart']";

    private static final String TOP_NAV = "//ul[@id='category-nav']";

    private static final String BOTTOM_NAV = "//div[@class='wrap']/ul[@class='links']";

    private static String JOIN_4_ACCESS_BTN = "//a[contains(@href, '/join?follow=') and text()='Join for Access']";

    private static String FOLLOW_ME_4_ACCESS_BTN = "//aside//input[@value='Follow Me for Access']";

    private static final String CATEGORY_DROPDOWN = "//li[@class='category cat-food dropdown']";

    private static final String OFFER_SELLER = "//div[@class='offer-info']/div[@class='seller']";

    private static final String OFFER_TITLE = "//div[@class='offer-info']/div[@class='title']";

    private static final String OFFER_URGENCY = "//div[@class='offer-info']/div[@class='urgency']";

    private static final String BUYIT4FRIEND_BTN = "//div[@class='buy-box-body']//input[@value='Buy it for a friend']";

    private static final String ADD2WISHLIST_BTN = "//div[@class='buy-box-body']//input[@name='wishlist']";

    private static final String RPODUCT_CHOISE_RADIO = "//form[@id='buy-box-form']//input[@type='radio']";

    private static final String RPODUCT_CHOISE_SELECT = "//form[@id='buy-box-form']//select";

    private static final String RPODUCT_CHOISE_LABEL = "//form[@id='buy-box-form']//span[@class='attribute select-attribute']//label[contains(@for, 'select_product')]";

    private static final String RPODUCT_FORM = "//form[@id='buy-box-form']";

    private String offerPageURL = "";

    // ************************ Complex page objects **************************

    private OfferPageJoinModal joinModal;

    private OfferPageJoinNag joinNag;

    private final WebDriverWrapper driver;

    public OfferPage(WebDriverWrapper driver, String curator, String product) {
	this.driver = driver;
	joinModal = new OfferPageJoinModal(driver);
	joinNag = new OfferPageJoinNag(driver);
	offerPageURL = String.format(URL_PATTERN, curator, product);
    }

    // ************************ Actions **************************

    @Override
    protected void load() {
	driver.get(offerPageURL);
    }

    public boolean isPageLoaded() {
	return driver.waitAndVerifyURL(offerPageURL);
    }

    public void hoverCategoryDropdown() {
	driver.waitAndHover("CATEGORY_DROPDOWN", By.xpath(CATEGORY_DROPDOWN));
    }

    public void clickJoin4AccessBtn() {
	driver.waitAndClick("JOIN4ACCESS_BTN", By.xpath(JOIN_4_ACCESS_BTN));
    }

    public CheckoutPage clickBuyIt4FriendBtn() {
	driver.waitAndCheckElementPresent("RPODUCT_FORM", By.xpath(RPODUCT_FORM));
	CheckoutPage checkoutPage = new CheckoutPage(driver);

	driver.waitAndClick("BUYIT4FRIEND_BTN", By.xpath(BUYIT4FRIEND_BTN));

	if (isCoisePresent()) {
	    driver.waitAndClick("BUYIT4FRIEND_BTN", By.xpath(BUYIT4FRIEND_BTN));
	}

	if (checkoutPage.isPageLoaded()) {
	    return checkoutPage;
	} else {
	    return null;
	}
    }

    public WishlistPage clickAdd2WishlistBtn() {

	driver.waitAndClick("ADD2WISHLIST_BTN", By.xpath(ADD2WISHLIST_BTN));

	if (isCoisePresent()) {
	    driver.waitAndClick("ADD2WISHLIST_BTN", By.xpath(ADD2WISHLIST_BTN));
	}

	WishlistPage wishlistPage = new WishlistPage(driver);

	if (wishlistPage.isPageLoaded()) {
	    return wishlistPage;
	} else {
	    return null;
	}
    }

    // ************************ Validations **************************

    @Override
    protected void isLoaded() throws Error {
	String url = driver.getCurrentUrl();
	Assert.assertTrue(AssertReport.wrongURL(offerPageURL, url), LogicUtils.isURLEqual(offerPageURL, url));
    }

    protected boolean isPageLoaded(String curator, String product) {
	String url = driver.getCurrentUrl();
	return LogicUtils.isURLEqual(url, String.format("URL_PATTERN", curator, product));
    }

    public boolean isJoin4AccessBtnPresent() {
	driver.waitAndCheckElementPresent("JOIN_4_ACCESS_BTN", By.xpath(JOIN_4_ACCESS_BTN));
	return true;
    }

    public boolean isFollowMe4AccessBtnPresent() {
	driver.waitAndCheckElementPresent("FOLLOW_ME_4_ACCESS_BTN", By.xpath(FOLLOW_ME_4_ACCESS_BTN));
	return true;
    }

    public boolean isIsBaseDropdownElementsPresent() {
	driver.waitAndCheckElementPresent("OFFER_SELLER", By.xpath(OFFER_SELLER));
	driver.waitAndCheckElementPresent("OFFER_TITLE", By.xpath(OFFER_TITLE));
	driver.waitAndCheckElementPresent("OFFER_URGENCY", By.xpath(OFFER_URGENCY));
	return true;
    }

    public boolean isBaseNavElementsPresent() {
	driver.waitAndCheckElementPresent("LOGIN_LINK", By.xpath(LOGIN_LINK));
	driver.waitAndCheckElementPresent("HELP_LINK", By.xpath(HELP_LINK));
	// driver.waitAndCheckElementPresent("CART_LINK", By.xpath(CART_LINK));
	driver.waitAndCheckElementPresent("TOP_NAV", By.xpath(TOP_NAV));
	driver.waitAndCheckElementPresent("BOTTOM_NAV", By.xpath(BOTTOM_NAV));
	return true;
    }

    private boolean isCoisePresent() {
	boolean result = false;

	WebElement choise;
	if (driver.findElements(By.xpath(RPODUCT_CHOISE_RADIO)).size() != 0) {
	    choise = driver.findElement(By.xpath(RPODUCT_CHOISE_RADIO));
	    choise.click();
	    result = true;
	} else if (driver.findElements(By.xpath(RPODUCT_CHOISE_SELECT)).size() != 0) {
	    choise = driver.findElement(By.xpath(RPODUCT_CHOISE_SELECT));
	    Select sel = new Select(choise);
	    sel.selectByIndex(1);
	    result = true;
	} else if (driver.findElements(By.xpath(RPODUCT_CHOISE_LABEL)).size() != 0) {
	    choise = driver.findElement(By.xpath(RPODUCT_CHOISE_LABEL));
	    choise.click();
	    result = true;
	}
	return result;
    }

    // ************************ GET/SET methods **************************

    public OfferPageJoinModal getJoinModal() {

	if (driver.waitAndVerifyElementPresent(By.xpath(JOIN_MODAL))) {
	    return joinModal;
	} else {
	    return null;
	}
    }

    public OfferPageJoinNag getJoinNag() {

	if (driver.waitAndVerifyElementPresent(By.xpath(JOIN_NAG))) {
	    return joinNag;
	} else {
	    return null;
	}
    }

}
